// 基础配置
import {
  mapMutations
} from "vuex";
import {
  onShow
} from "@dcloudio/uni-app";

// export const BASE_URL = "http://test.api.ylnumber.cn"; // 本地测试环境 API 地址
// export const BASE_URL = "https://888.53938.com/nya/api/yin"; // 本地测试环境 API 地址
// export const BASE_URL_IMAHE = "https://888.53938.com/"; // 本地测试环境 图片 和视频 地址？

export const BASE_URL = "https://niya.shfrei.com/nya/api/yin" // 线上正式环境 API 地址
export const BASE_URL_IMAHE = "https://niya.shfrei.com" // 线上正式环境 图片 和视频 地址

import store from "./store";
// 当前是否显示modal
let loginModal = false;
const token = uni.getStorageSync("token");
class HZCRequest {
  request(url, method, params, header = url.indexOf("http://124.156.185.241/h5Shop/") != -1 ? {
    "Content-Type": "application/json;charset=UTF-8",

  } : {
    "Content-Type": "application/json;charset=UTF-8",
  }) {
    // 获取到登录后的token

    uni.showLoading({
      title: "加载中...",
    });
    return new Promise((resolve, reject) => {
      uni.request({
        url: url.indexOf("http://124.156.185.241/h5Shop/") != -1 ? url : BASE_URL + url,
        method: method || "GET",
        header,
        data: params,
        success: (res) => {
          console.log(res, "res");
          //关闭加载动画
          uni.stopPullDownRefresh();
          uni.hideLoading();
          // 判断是否为报错码 后期自己根据设计添加
          if (res.data.code === 200 || (res.data.code === 0 && res.data.msg)) {
            if (res.data.msg.length > 6) {
              uni.showToast({
                title: "操作成功",
                icon: "success",
                duration: 2000,
              });
            } else {
              if (res.data.msg !== "添加成功" && res.data.msg !== "查询成功") {
                uni.showToast({
                  title: res.data.msg,
                  icon: "success",
                  duration: 2000,
                });
              }
            }
          } else if (res.data.code === 401) {
            // 暂时不处理未登录的
            return
            // uni.showToast({
            //   title: "登录过期，请重登",
            //   icon: "error",
            //   duration: 2000,
            // });
            uni.removeStorageSync("token");
            uni.removeStorageSync("userInfoServer");
            uni.removeStorageSync("userId");
            uni.removeStorageSync("openId");
            uni.removeStorageSync("MyUserInfo");
            uni.removeStorageSync("phoneNumber");
            uni.removeStorageSync("roleId");

            // 判断是否需要登录
            // 防止重复弹窗
            if (!loginModal) {
              loginModal = true;
              // 弹窗告诉用户去登录
              uni.showModal({
                title: "温馨提示",
                content: "此时此刻需要您登录喔~",
                // showCancel: false,
                confirmText: "去登录",
                cancelText: "再逛会",
                success: (res) => {
                  if (res.confirm) {
                    uni.navigateTo({
                      url: "/pages/empower/mp-weixin",
                    });
                  }
                  if (res.cancel && getCurrentPages().length >
                    1) {
                    uni.navigateBack();
                  }
                  loginModal = false;
                },
              });
            }
          } else if (res.data.code === 1) {
            return
            let sf = StorageSync("roleId") == 5 ? "普通用户" : StorageSync("roleId") ==
              4 ? "店主身份" :
              StorageSync("roleId") == 3 ? "服务商" : StorageSync("roleId") == 2 ?
                "合伙人" : StorageSync("roleId") == 1 ? "联创" : ""
            uni.showToast({
              title: "您已经是" + sf,
              icon: "error",
              duration: 2000,
            });
          } else {
            if (res.data.msg) {
              uni.showToast({
                title: res.data.msg,
                icon: "error",
                duration: 2000,
              });
            }
          }

          // 返回请求值
          resolve(res.data);
        },
        fail: (err) => {
          setTimeout(() => {
            uni.hideLoading();
            uni.showToast({
              title: "网络异常或请求错误",
              icon: "error",
              duration: 2000,
            });
          }, 3000);
        },
      });
    });
  }

  get(url, params, header) {
    //判断是否有网络
    uni.getNetworkType({
      success: function (res) {
        if (res.networkType === "none") {
          uni.showToast({
            icon: "none",
            title: "网络异常，请检测网络配置!",
            duration: 2000,
          });
        }
      },
    });
    return this.request(url, "GET", params, header);
  }
  post(url, data, header) {
    //判断是否有网络
    uni.getNetworkType({
      success: function (res) {
        console.log(res, "网络");
        if (res.networkType === "none") {
          uni.showToast({
            icon: "none",
            title: "网络异常，请检测网络配置!",
            duration: 2000,
          });
        }
      },
    });
    return this.request(url, "POST", data, header);
  }
  put(url, data, header) {
    //判断是否有网络
    uni.getNetworkType({
      success: function (res) {
        console.log(res, "网络");
        if (res.networkType === "none") {
          uni.showToast({
            icon: "none",
            title: "网络异常，请检测网络配置!",
            duration: 2000,
          });
        }
      },
    });
    return this.request(url, "PUT", data, header);
  }
  delete(url, data, header) {
    //判断是否有网络
    uni.getNetworkType({
      success: function (res) {
        console.log(res, "网络");
        if (res.networkType === "none") {
          uni.showToast({
            icon: "none",
            title: "网络异常，请检测网络配置!",
            duration: 2000,
          });
        }
      },
    });
    return this.request(url, "DELETE", data, header);
  }
}

const hzcRequest = new HZCRequest();

// ['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
//   hzcRequest[method] = (url, data, opt, params) => this.request(url, method, data, opt || {}, params)
// });

export default hzcRequest;